<%@ page import="java.io.File" %>
<%@ page import="static com.sun.xml.internal.bind.WhiteSpaceProcessor.replace" %><%--
  Created by IntelliJ IDEA.
  User: Fan
  Date: 2016/10/17
  Time: 9:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <title>列表</title>
    <style type="text/css">
        body{
            background: url(/images/background3.jpg);
            background-size: 100%;
        }
        h4{
            color:dodgerblue;
        }
        #back{

            float: left;
            margin-left: 70px;
        }
        #forward{
            float: right;
            margin-right: 70px;
        }
        .bottom{
            position: absolute;
            bottom: 50px;
            left: 0;
            height: 20px;
            width: 100%;
        }
    </style>
    <script language="JavaScript">
        //平台、设备和操作系统（windows系统，苹果系统，Linux或者X11系统）
        var system ={
            win : false,
            mac : false,
//            x11 : false
//            xll_1 : false, //安卓手机
//            x11_2 : false  //苹果手机
        };
        //检测平台
        var p = navigator.platform;

        //利用p可以判断手机还是电脑，但是无法区分安卓和ios（即手机端），最终用p1判断是ios还是安卓
        var p1 = navigator.userAgent.toLowerCase();
        //alert(p1);
        system.win = p.indexOf("Win") == 0; //如果是windows系统就显示true，否则false。
        system.mac = p.indexOf("Mac") == 0;  //如果是Mac系统就显示true，否则false。
//        system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);  //如果是Linux或者X11系统就显示true，否则false。
//        system.xll_1 = p.indexOf("Linux") == 0;
//        system.x11_2 = ((p == "X11") ==0);

        function play(filepath) {
//        alert(fileName);
//        var suffix = /\.[^\.]+/.exec(fileName);
//        alert(suffix);
            var extension = filepath.substring(filepath.lastIndexOf('.'),filepath.length).toLowerCase();
//        alert(extension);
            if(extension == ".mp4")
            {
                window.location.href="/Pages/displayVideo.jsp?filepath=" + filepath;
            }
            //如果是电脑打开网页的，直接把整个pdf加载
//            else if((extension == ".pdf") && (system.win||system.mac||system.x11_2))
//            {
//                //alert( system.win||system.mac||system.xll);
//                window.location.href="/Pages/displayPdfPc.jsp?filepath=" + filepath;
//            }
//            else if((extension == ".pdf") && (system.xll_1))
//            {
//                alert(system.xll_1);
//                window.location.href="/Pages/displayPdfMobile.jsp?filepath=" + filepath;
//            }
              else if(extension == ".pdf")
              {
                  if(system.win||system.mac) {
//                      alert("win : " + system.win);
//                      alert("mac :" + system.mac);
                      window.location.href = "/Pages/displayPdfPc.jsp?filepath=" + filepath;
                  }
//                  else if(system.x11_2) {
//                      alert("X11_2 : " + system.x11_2);
//                      alert("X11_1 : " + system.xll_1);
//                      window.location.href = "/Pages/displayPdfPc.jsp?filepath=" + filepath;
//                  }
////                  else if(system.xll_1) {
//                  else {
//                      alert("X11_1 : " + system.xll_1);
//                      alert("X11_2 : " + system.x11_2);
//                      window.location.href = "/Pages/displayPdfMobile.jsp?filepath=" + filepath;
//                  }
                    else{
                      if (/iphone|ipad|ipod/.test(p1)) {
                          //alert("iphone");
                          window.location.href = "/Pages/displayPdfPc.jsp?filepath=" + filepath;
                      } else if (/android/.test(p1)) {
                          //alert("android");
                          window.location.href = "/Pages/displayPdfMobile.jsp?filepath=" + filepath;
                      }
                  }
              }
//            //如果是用手机打开网页的话，跳转页面，需要下载
//            else if(extension == ".pdf")
//            {
//                window.location.href="/Pages/displayPdfMobile.jsp?filepath=" + filepath;
//            }
            else if( (extension == ".doc") || (extension == ".docx"))
            {
                window.location.href="/Pages/displayDoc.jsp?filepath=" + filepath;
            }
            else if((extension == ".ppt") || (extension == ".pptx"))
            {
                window.location.href="/Pages/displayDoc.jsp?filepath=" + filepath;
            }
            else if(extension == ".txt")
            {
                //window.location.href="/Pages/displayTxt.jsp?filepath=" + filepath;
            }
            else
            {
                alert("操作错误，请重新选择！")
            }
        }
    </script>
</head>

<body>
    <h2>文件列表</h2>
    <%--各种路径--%>
    <%--获取Web应用的物理路径即绝对路径：<%=application.getRealPath("/")%><br/>--%>
    <%--当前你求请的JSP文件的物理路径：<%=application.getRealPath(request.getRequestURI())%><br/>--%>
    <%--获取res目录路径："<%=request.getContextPath()%>/res"<br/>--%>
    <%--根目录所对应的绝对路径:<%=request.getRequestURI()%><br>--%>

    <%
//        System.out.println(request.getParameter("path"));
        String sPath= application.getRealPath("/") + request.getParameter("path");
        System.out.println("listFile.jsp 选择的四个分类路径：" + sPath);
        //ClassLoader
        File file = new File(sPath);
//        file.separatorChar
        File[] files = file.listFiles();
        if(files != null)
        {
            %>
            <h4 style="text-decoration:underline" ><img src="/images/tag_fill_16x16.png">视频列表</h4>
            <%
            for(File f :files)
            {
                // 之前采用的是绝对路径，不能播放本地资源
//                String str=sPath +"\\" +f.getName();
//                System.out.println(str);
                //采用相对路径
                String pathFile = ".." + "\\" + request.getParameter("path") + "\\" + f.getName();
//                System.out.println(pathFile);
                //在将路径传给js函数中时，先将“\”转换成“\\”
//                String str1=str.replaceAll("\\\\","\\\\\\\\");
                String str1=pathFile.replaceAll("\\\\","\\\\\\\\");
//            out.print("<br>");
                String suffix = str1.substring(str1.lastIndexOf('.')+1).toLowerCase();
//                System.out.println(suffix);
                if(suffix.equals("mp4"))
                {
            %>

                    <label class="video" onmouseover="this.style.color='red'" onmouseout="this.style.color='#000000'"
                             style="cursor: pointer" onclick="play('<%=str1%>')"><%=f.getName()%> </label><br>
            <%
                }
//                out.println(f.getName());
//                out.print("<br>");
            }
            //显示PDF列表
            %>
                <h4 style="text-decoration:underline" ><img src="/images/tag_fill_16x16.png">PDF列表</h4>
            <%
            for(File f :files)
            {
                    String pathFile = ".." + "\\" + request.getParameter("path") + "\\" + f.getName();
                    String str1=pathFile.replaceAll("\\\\","\\\\\\\\");
                    String suffix = str1.substring(str1.lastIndexOf('.')+1).toLowerCase();
                    if(suffix.equals("pdf"))
                    {
            %>
            <label class="video" onmouseover="this.style.color='red'" onmouseout="this.style.color='#000000'"
                   style="cursor: pointer" onclick="play('<%=str1%>')"><%=f.getName()%> </label><br>
            <%
                    }
            }
            //显示office文件列表
            %>
            <h4 style="text-decoration:underline" ><img src="/images/tag_fill_16x16.png">Office文件列表</h4>
            <%
            for(File f :files)
            {
                String pathFile = ".." + "\\" + request.getParameter("path") + "\\" + f.getName();
                String str1=pathFile.replaceAll("\\\\","\\\\\\\\");
                String suffix = str1.substring(str1.lastIndexOf('.')+1).toLowerCase();
                if(!suffix.equals("mp4") && !suffix.equals("pdf"))
                {
            %>
            <label class="video" onmouseover="this.style.color='red'" onmouseout="this.style.color='#000000'"
                   style="cursor: pointer" onclick="play('<%=str1%>')"><%=f.getName()%> </label><br>
            <%
                }
            }

        }
        else
        {
            response.sendRedirect("error1.jsp" );
        }
            %>
    <div class="bottom"><img id="back" src="/images/arrow_left_24x24.png" onclick="window.history.back()"><img id="forward" src="/images/arrow_right_24x24.png" onclick="window.history.forward()"></div>
</body>
</html>
